<?php
/**
 * @Notes:【】
 * @User: zwc
 * @Date: 2022-05-14 0:20
 */

namespace App\Service\Chat;

use App\Constants\ChatEvent;
use App\Dao\Chat\ChatRecordsDao;
use App\Dao\Chat\ChatSessionDao;

use App\Resource\ChatFriends;
use App\WsService\Message\BaseService;
use Hyperf\Di\Annotation\Inject;

class ChatSessionService extends BaseService
{

    /**
     * @Inject()
     * @var ChatSessionDao
     */
    protected $chatSessionDao;

    public function lists($userId)
    {
        $data = $this->chatSessionDao
            ->DaoWhere(['user_id' => $userId])
            ->withCount(['receiverFd' => function ($query) {
                $query->where('server_run_id', _Cache()->get('SERVER_RUN_ID'));
            }])->with(['User', 'Message', 'Receiver'])->get();
        $data->transform(function ($item) {
            $item->unReadNum = App(ChatRecordsDao::class)->where('user_id', $item->receiver_id)->where('receiver_id', $item->user_id)->count();
            return $item;
        });
        $data = (new ChatFriends($data));
//        $data = $this->chatSessionDao
//            ->DaoWhere(['user_id' => $userId])
//            ->withCount(['receiverFd' => function ($query) {
//                $query->where('server_run_id', _Cache()->get('SERVER_RUN_ID'));
//            }, 'ChatRecords' => function ($query) use ($userId) {
//                $query->where('user_id', $userId);
//            }])
//            ->with(['User', 'Message', 'Receiver'])->getList();
//
        return $data;
    }

}